
#ifndef _RING_CHUNK_H__
#define _RING_CHUNK_H__

//环形区块

typedef struct {
	uint32_t baseSector;
	uint32_t itemCapacity;
	uint32_t head;
	uint32_t tail;
	uint32_t cursor;
	uint32_t size;
	uint32_t maxSize;
	uint32_t infoIndex;
	uint32_t infoVersion;
	uint32_t infoBaseSector;
	uint32_t infoCapacity;
	uint8_t itemPerPage;
	uint8_t itemSize;
	uint8_t changed;
}RingChunk_T;

typedef struct {
	uint32_t id;
	uint8_t data[4];
}RingChunkItem_T;

typedef struct {
	uint32_t version;
	uint32_t head;
	uint32_t tail;
	uint16_t count;
	uint16_t crc;
}RingChunkInfo_T;

enum {
	R_SEEK_START,
	R_SEEK_END,
	R_SEEK_CUR,	
};


#if 0
void RingChunk_Init(RingChunk_T* chunk, uint32_t base,
	uint32_t sectorNum, uint32_t head, uint32_t tail, uint8_t itemSize);
#endif 

int RingChunk_Read(RingChunk_T* chunk, uint32_t index, void* buffer, uint32_t size);
int RingChunk_CachRead(RingChunk_T* chunk, uint32_t index, void* buffer, uint32_t size);
int RingChunk_ReadItem(RingChunk_T* chunk, void* item);
int RingChunk_CachReadItem(RingChunk_T* chunk, void* item);
int RingChunk_PreviousReadItem(RingChunk_T* chunk, void* item);
int RingChunk_Write(RingChunk_T* chunk, uint32_t index, void* buffer, uint32_t size);
uint32_t RingChunk_Address(RingChunk_T* chunk, uint32_t index);
int RingChunk_IsFull(RingChunk_T* chunk);
uint32_t RingChunk_Footprint(RingChunk_T* chunk);
uint32_t RingChunk_Seek(RingChunk_T* chunk, uint32_t offset, uint8_t flag);
void RingChunk_Clear(RingChunk_T* chunk);
int RingChunk_FindNext(RingChunk_T* chunk, const uint32_t* id, uint32_t* index);
int RingChunk_FindPrevious(RingChunk_T* chunk, const uint32_t* id, uint32_t* index);
int RingChunk_DirFind(RingChunk_T* chunk, uint8_t dir, const uint32_t* id, uint32_t* index);
int RingChunk_FindItem(RingChunk_T* chunk, uint8_t dir, uint32_t id, void* item, uint32_t* index);
int RingChunk_FindItemNext(RingChunk_T* chunk, uint32_t id, void* item, uint32_t* index);
int RingChunk_Append(RingChunk_T* chunk, void* item);
int RingChunk_Remove(RingChunk_T* chunk, uint32_t id);
int RingChunk_RemoveAt(RingChunk_T* chunk, uint32_t index);
void RingChunk_UpdateInfo(RingChunk_T* chunk);
void RingChunk_Setup(RingChunk_T* chunk, uint32_t infoSec, uint32_t infoSecNum,
	uint32_t dataSec, uint32_t dataSecNum, uint8_t itemSize, uint32_t maxSize);


#endif



